/**
 * GWT Kad : Kademlia implementation for GWT
 * Copyright (C) 2012  Pierrick HYMBERT, Paul-André MISTRAL, Florian ALEXANDRE
 * 
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 * 
 */
package org.kad.gwt.web.client.transport.http.listeners;

import javax.inject.Inject;

import org.kad.gwt.web.client.applet.AppletListener;
import org.kad.gwt.web.client.mvp.AbstractHasHandlers;
import org.kad.gwt.web.client.transport.http.message.HttpMessageRequest;
import org.kad.gwt.web.client.transport.http.message.HttpMessageResponse;
import org.kad.gwt.web.client.transport.http.mvp.message.HasHttpServerMessageHandlers;
import org.kad.gwt.web.client.transport.http.mvp.message.HttpServerMessageEvent;

import com.google.gwt.core.client.JavaScriptObject;
import com.google.gwt.event.shared.EventBus;
/**
 * The gwt kad applet listener a bridge to the gwt mvp acrhitecture.
 *
 * @author Pierrick HYMBERT &lt;pierrick [dot] hymbert [at] gmail [dot] com&gt;
 */
public class GWTKadAppletMessageListener extends AbstractHasHandlers implements AppletListener, HasHttpServerMessageHandlers {
    /**
     * Allow the conversion of GWT <code>GWTKadAppletClient</code> to <code>AppletListenerJSAdapter</code>.
     */
    private JavaScriptObject setCallBack;
    /**
     * Constructor.
     * 
     * @param eventBusIn
     *            The event bus
     */
    @Inject
    public GWTKadAppletMessageListener(final EventBus eventBusIn) {
        super(eventBusIn);
        enhanceByJsni();
    }

    /**
     * Create the {@link #callBackSetter}.
     */
    private native void enhanceByJsni() /*-{
        this.@org.kad.gwt.web.client.transport.http.listeners.GWTKadAppletMessageListener::setCallBack = function(callBack) {
            $wnd.gwtKadMessageCallback = callBack;
        }
    }-*/;
    /**
     * {@inheritDoc}
     */
    @Override
    public void handle(final Object param1, final Object param2, final Object param3) {
        HttpMessageRequest request = new HttpMessageRequest((String) param1, (String) param2);
        HttpMessageResponse response = new HttpMessageResponse(404, "Not found");
        HttpServerMessageEvent.fire(this, request, response);
        sendResponse(response.getResponseCode(), response.getMessage());
    }
    /**
     * Send the response to the server.
     * 
     * @param responseCode The response code
     * @param response The response message
     */
    native void sendResponse(int responseCode, String response) /*-{
        try {
            $wnd.gwtKadMessageCallback.answer(responseCode, response);
        } catch (e) {alert(e)}
    }-*/;
}